package com.jinlejia.erp.ui.mine.key;

import android.content.Intent;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.databinding.ActivityCheckPictureBinding;
import com.jinlejia.erp.ui.common.ImageViewPagerActivity;
import com.jinlejia.erp.ui.mine.adapter.CheckPictureAdapter;

import java.util.ArrayList;


/**
 * 查看图片（activity）
 */

public class CheckPictureActivity extends BaseActivity {
    private CheckPictureAdapter adapter;
    private ArrayList<String> dataList = new ArrayList<>();

    private ActivityCheckPictureBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_check_picture);
        initView();
        initEvent();
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        adapter.OnImageListener(position -> {
            Intent intent = new Intent(CheckPictureActivity.this, ImageViewPagerActivity.class);
            switch (getIntent().getIntExtra("type", 0)) {
                case 0:
                    intent.putStringArrayListExtra("imgs", dataList);
                    intent.putExtra("check", "picture");
                    intent.putExtra("position", position + 1);
                    break;
                case 1:
                    intent.putStringArrayListExtra("imgs", dataList);
                    intent.putExtra("check", "picture");
                    intent.putExtra("position", position + 1);
                    break;
                case 2:
                    intent.putStringArrayListExtra("imgs", dataList);
                    intent.putExtra("check", "picture");
                    intent.putExtra("position", position + 1);
                    break;
                case 3:
                    intent.putStringArrayListExtra("imgs", dataList);
                    intent.putExtra("check", "picture");
                    intent.putExtra("position", position + 1);
                    break;
            }
            startActivity(intent);
        });
    }

    /**
     * 初始化视图
     */
    private void initView() {
        dataList.clear();
        switch (getIntent().getIntExtra("type", 0)) {
            case 0:
                binding.title.setText("钥匙图片");
                if (null != getIntent().getStringArrayListExtra("sellinglist")) {
                    ArrayList<String> sellinglist = getIntent().getStringArrayListExtra("sellinglist");
                    for (int i = 0; i < sellinglist.size(); i++) {
                        dataList.add(ApiConfig.MAGEHEADER_URL().concat(sellinglist.get(i)));
                    }
                }
                break;
            case 1:
                binding.title.setText("钥匙图片");
                if (null != getIntent().getStringArrayListExtra("rentlist")) {
                    ArrayList<String> rentlist = getIntent().getStringArrayListExtra("rentlist");
                    for (int i = 0; i < rentlist.size(); i++) {
                        dataList.add(ApiConfig.MAGEHEADER_URL().concat(rentlist.get(i)));
                    }
                }
                break;
            case 2:
                binding.title.setText("实勘图片");
                if (null != getIntent().getStringArrayListExtra("keyselling")) {
                    ArrayList<String> keyselling = getIntent().getStringArrayListExtra("keyselling");
                    for (int i = 0; i < keyselling.size(); i++) {
                        dataList.add(ApiConfig.MAGEHEADER_URL().concat(keyselling.get(i)));
                    }
                }
                break;
            case 3:
                binding.title.setText("实勘图片");
                if (null != getIntent().getStringArrayListExtra("keyrent")) {
                    ArrayList<String> keyrent = getIntent().getStringArrayListExtra("keyrent");
                    for (int i = 0; i < keyrent.size(); i++) {
                        dataList.add(ApiConfig.MAGEHEADER_URL().concat(keyrent.get(i)));
                    }
                }
                break;
        }

        binding.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        binding.recyclerView.setHasFixedSize(true);
        adapter = new CheckPictureAdapter(dataList);
        binding.recyclerView.setAdapter(adapter);
        binding.backBtn.setOnClickListener(view -> finish());
    }

}
